package android.slc.box.core.utils;

import android.slc.box.core.controller.EventPlusHandler;
import javafx.scene.Node;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;
import javafx.stage.WindowEvent;

public class WindowUtils {

    /**
     * 标题栏高度
     */
    private static double titleBarHeight = 0;

    /**
     * 关闭窗口
     *
     * @param window
     */
    public static void destroy(Window window) {
        if (window != null) {
            window.fireEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSE_REQUEST));
        }
    }

    public static EventPlusHandler getOnWindowCloseRequest(Window window) {
        if (window == null) {
            return null;
        }
        if (window.getOnCloseRequest() instanceof EventPlusHandler) {
            EventPlusHandler onCloseEventHandler = (EventPlusHandler) window.getOnCloseRequest();
            return onCloseEventHandler;
        } else {
            EventPlusHandler onCloseEventHandler = new EventPlusHandler();
            window.setOnCloseRequest(onCloseEventHandler);
            return onCloseEventHandler;
        }
    }

    public static void addOnWindowCloseRequest(Window window, EventPlusHandler.EventPlusHandlerListener eventPlusHandlerListener) {
        EventPlusHandler eventPlusHandler = getOnWindowCloseRequest(window);
        if (eventPlusHandler != null) {
            eventPlusHandler.addEventPlusHandlerListener(eventPlusHandlerListener);
        }
    }

    public static void removeOnWindowCloseRequest(Window window, EventPlusHandler.EventPlusHandlerListener eventPlusHandlerListener) {
        EventPlusHandler eventPlusHandler = getOnWindowCloseRequest(window);
        if (eventPlusHandler != null) {
            eventPlusHandler.removeEventPlusHandlerListener(eventPlusHandlerListener);
        }
    }

    /**
     * 获取标题栏高度
     *
     * @param window
     * @return
     */
    public static double getTitleBarHeight(Window window) {
        if (window == null) {
            return 0;
        }
        double windowHeight = window.getHeight();
        if (window.getScene() != null) {
            double sceneHeight = window.getScene().getHeight();
            return windowHeight - sceneHeight;
        }
        return 0;
    }

    /**
     * 根据窗口设置全局的标题栏高度
     *
     * @param window
     */
    public static void setGlobalTitleBarHeight(Window window) {
        setGlobalTitleBarHeight(window, false);
    }

    /**
     * 根据窗口设置全局的标题栏高度
     *
     * @param window
     * @param cover
     */
    public static void setGlobalTitleBarHeight(Window window, boolean cover) {
        if (window == null) {
            return;
        }
        if (getGlobalTitleBarHeight() == 0 || cover) {
            setGlobalTitleBarHeight(getTitleBarHeight(window), cover);
        }
    }

    /**
     * 设置全局标题栏高度
     *
     * @param height
     */
    public static void setGlobalTitleBarHeight(double height) {
        setGlobalTitleBarHeight(height, false);
    }

    /**
     * 设置全局标题栏高度
     *
     * @param height
     * @param cover
     */
    public static void setGlobalTitleBarHeight(double height, boolean cover) {
        if (getGlobalTitleBarHeight() == 0 || cover) {
            titleBarHeight = height;
        }
    }

    /**
     * 获取标题栏高度
     *
     * @return
     */
    public static double getGlobalTitleBarHeight() {
        return titleBarHeight;
    }

    /**
     * 获取标题栏高度
     * 传入Stage进行决定，如果窗口的是全屏的，则没有高度，如果窗口Style不是DECORATED或UTILITY，也将没有高度，如果当前全局的高度0，则根据次Stage设置全局标题栏高度
     *
     * @param stage
     * @return
     */
    public static double getGlobalTitleBarHeight(Stage stage) {
        if (stage.isFullScreen()) {
            return 0;
        }
        if (stage.getStyle() != StageStyle.DECORATED && stage.getStyle() != StageStyle.UTILITY) {
            return 0;
        }
        if (getGlobalTitleBarHeight() == 0) {
            setGlobalTitleBarHeight(stage);
        }
        return getGlobalTitleBarHeight();
    }

    /**
     * 调整窗口大小
     * @param node
     * @param window
     */
    public static void adjustWindowSize(Node node, Window window) {
        // 获取内容标签的首选宽度和高度
        double preferredWidth = node.prefWidth(Region.USE_COMPUTED_SIZE);
        double preferredHeight = node.prefHeight(Region.USE_COMPUTED_SIZE);

        // 调整窗口的最小大小
        window.setWidth(preferredWidth);
        if(window instanceof Stage){
            window.setHeight(preferredHeight + getGlobalTitleBarHeight((Stage) window));
        }else{
            window.setHeight(preferredHeight + getGlobalTitleBarHeight());
        }
    }
}
